﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace DDNY.WebPortals.Common
{
    public class CommonUtils
    {
        public static string SanitizeContent(string htmlContent, bool cropImages)
        {
            StringBuilder result = new StringBuilder();
            result.Append(htmlContent);
            result.Replace(@"a href", @"a rel=""nofollow"" href");
            if (cropImages)
            {
                result.Replace("<img ", @"<img height=""40px"" width=""60px"" ");
            }
            return result.ToString();
        }

        public static string GetExcerpt(string htmlContent)
        {
            int excerptLength = 150;
            string result = htmlContent;
            if (htmlContent != null && htmlContent.Length > excerptLength)
            {
                return htmlContent.Substring(0, excerptLength) + " ...";
            }
            return result;
        }


        public static string StripHtml(string p)
        {

            return Regex.Replace(p, @"<(.|\n)*?>", string.Empty);

        }



        public static bool CheckIpAddressAllowed(string ipAddress)
        {
            bool result=false;  
            List<string> allowedIPs = new List<string>();
            allowedIPs.Add("91.196.125.129");
            allowedIPs.Add("127.0.0.1");
            allowedIPs.Add("92.247.17.170");
            
            if (allowedIPs.Contains(ipAddress))
            {
                result = true;
            }
            return result;
        }

        public static string GetIPAddress(HttpRequest request)
        {
            string sIPAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(sIPAddress))
            {
                sIPAddress = request.ServerVariables["REMOTE_ADDR"];

            }
            else
            {
                string[] ipArray = sIPAddress.Split(',');
                sIPAddress = ipArray[0];
            }
            return sIPAddress;
        }

    }
}
